package com.game.controller;

import com.game.model.map.GameMap;
import com.game.model.map.MapPoint;
import com.game.model.role.PlayerRole;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:地图控制器
 **/
public class MapController {
    //找到角色在地图上位置
    public static MapPoint getRoleInMap(GameMap gameMap) {
        MapPoint[][] mapPoints = gameMap.getMapPoints();
        for (int i = 0; i < mapPoints.length; i++) {
            for (int j = 0; j < mapPoints[i].length; j++) {
                if (mapPoints[i][j].getPointObject() != null) {
                    for (Object o : mapPoints[i][j].getPointObject()) {
                        if (o instanceof PlayerRole) {
                            return mapPoints[i][j];
                        }
                    }
                }
            }
        }
        return null;
    }

    //添加物体到地图某点
    public static GameMap addObjectInMap(GameMap gameMap, int x, int y, Object o) {
        MapPoint[][] mapPoints = gameMap.getMapPoints();
        List<Object> pointObject = mapPoints[x][y].getPointObject();
        if (pointObject == null) {
            List<Object> objects = new ArrayList<>();
            objects.add(o);
            mapPoints[x][y].setPointObject(objects);
            gameMap.setMapPoints(mapPoints);
        } else {
            pointObject.add(o);
            mapPoints[x][y].setPointObject(pointObject);
            gameMap.setMapPoints(mapPoints);
        }
        return gameMap;
    }

    //移除在地图点上的某物体
    public static GameMap removeObjectInMap(GameMap gameMap, int x, int y, Object o) {
        MapPoint[][] mapPoints = gameMap.getMapPoints();
        List<Object> pointObject = mapPoints[x][y].getPointObject();
        if (pointObject != null) {
            for (int i = pointObject.size() - 1; i >= 0; i--) {
                //如果有o这个要删除的就删除掉
                if (pointObject.get(i) == o || pointObject.get(i).equals(o)) {
                    pointObject.remove(i);
                }
            }
            mapPoints[x][y].setPointObject(pointObject);
            gameMap.setMapPoints(mapPoints);
        }
        return gameMap;
    }
}
